package com.smart.campus.school.service.impl;

import java.util.Date;
import java.util.List;
import com.smart.campus.common.utils.DateUtils;
import com.smart.campus.school.common.CommonConstants;
import com.smart.campus.school.domain.Msg;
import com.smart.campus.school.domain.Student;
import com.smart.campus.school.domain.query.IllegalRecordQuery;
import com.smart.campus.school.domain.vo.IllegalRecordReportVo;
import com.smart.campus.school.domain.vo.IllegalRecordVo;
import com.smart.campus.school.enums.MsgTypeEnum;
import com.smart.campus.school.mapper.MsgMapper;
import com.smart.campus.school.mapper.StudentMapper;
import com.smart.campus.school.service.IMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smart.campus.school.mapper.IllegalRecordMapper;
import com.smart.campus.school.domain.IllegalRecord;
import com.smart.campus.school.service.IIllegalRecordService;
import com.smart.campus.common.core.text.Convert;

/**
 * 违规记录Service业务层处理
 * 
 * @author Zhihe.Li
 * @date 2020-05-25
 */
@Service
public class IllegalRecordServiceImpl implements IIllegalRecordService 
{
    @Autowired
    private IllegalRecordMapper illegalRecordMapper;

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private IMsgService msgService;

    /**
     * 查询违规记录
     * 
     * @param id 违规记录ID
     * @return 违规记录
     */
    @Override
    public IllegalRecordVo selectIllegalRecordById(Long id)
    {
        return illegalRecordMapper.selectIllegalRecordById(id);
    }

    /**
     * 查询违规记录列表
     * 
     * @param query 违规记录
     * @return 违规记录
     */
    @Override
    public List<IllegalRecordVo> selectIllegalRecordList(IllegalRecordQuery query)
    {
        return illegalRecordMapper.selectIllegalRecordList(query);
    }

    /**
     * 新增违规记录
     * 
     * @param illegalRecord 违规记录
     * @return 结果
     */
    @Override
    public int insertIllegalRecord(IllegalRecord illegalRecord)
    {
        illegalRecord.setCreateTime(DateUtils.getNowDate());
        Student student = studentMapper.selectStudentById(illegalRecord.getStudentId());
        illegalRecord.setGradeId(student.getGradeId());
        illegalRecord.setGradeName(student.getGradeName());
        illegalRecord.setClassId(student.getClassId());
        illegalRecord.setClassName(student.getClassName());
        illegalRecord.setStudentName(student.getStudentName());
        int res = illegalRecordMapper.insertIllegalRecord(illegalRecord);
        Msg msg = new Msg();
        msg.setUserType(CommonConstants.STUDENT);
        msg.setStatus(CommonConstants.NO);
        msg.setCreateTime(new Date());
        msg.setUserId(illegalRecord.getStudentId());
        msg.setUserName(student.getStudentName());
        msg.setContent("您有一条违规记录!");
        msg.setMsgType(MsgTypeEnum.MSG.getValue());
        msgService.insertMsg(msg);
        return res;
    }

    /**
     * 修改违规记录
     * 
     * @param illegalRecord 违规记录
     * @return 结果
     */
    @Override
    public int updateIllegalRecord(IllegalRecord illegalRecord)
    {
        illegalRecord.setUpdateTime(DateUtils.getNowDate());
        return illegalRecordMapper.updateIllegalRecord(illegalRecord);
    }

    /**
     * 删除违规记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteIllegalRecordByIds(String ids)
    {
        return illegalRecordMapper.deleteIllegalRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除违规记录信息
     * 
     * @param id 违规记录ID
     * @return 结果
     */
    @Override
    public int deleteIllegalRecordById(Long id)
    {
        return illegalRecordMapper.deleteIllegalRecordById(id);
    }

    /**
     * 查询违规记录统计
     *
     * @param query 违规记录
     * @return 违规记录集合
     */
    @Override
    public List<IllegalRecordReportVo> selectIllegalRecordReport(IllegalRecordQuery query) {
        return illegalRecordMapper.selectIllegalRecordReport(query);
    }
}
